package com.hc.demo.dao.impl;

import com.hc.demo.dao.JdbcTemplateDeptDao;
import com.hc.demo.domain.JdbcTemplateDept;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class JdbcTemplateDeptDaoImpl implements JdbcTemplateDeptDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(JdbcTemplateDept dept) {
        String sql = "insert into tb_dept values (?,?,?)";
        int res = jdbcTemplate.update(sql, dept.getDeptno(), dept.getDname(), dept.getLoc());
        return res;
    }

    @Override
    public int update(JdbcTemplateDept dept) {
        String sql = "update tb_dept set dname = ?, loc = ? where deptno = ?";
        int res = jdbcTemplate.update(sql, dept.getDname(), dept.getLoc(), dept.getDeptno());
        return res;
    }

    @Override
    public int delete(Integer deptno) {
        String sql = "delete from tb_dept where deptno = ?";
        int res = jdbcTemplate.update(sql, deptno);
        return res;
    }

    @Override
    public JdbcTemplateDept selectByDeptno(Integer deptno) {
        String sql = "select * from tb_dept where deptno = ?";
        JdbcTemplateDept dept = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(JdbcTemplateDept.class), deptno);
        return dept;
    }

    @Override
    public List<JdbcTemplateDept> selectAll() {
        String sql = "select * from tb_dept";
        List<JdbcTemplateDept> res = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(JdbcTemplateDept.class));
        return res;
    }

    @Override
    public List<Integer> selectAllDeptnos() {
        String sql = "select deptno from tb_dept";
        List<Integer> res = jdbcTemplate.queryForList(sql, Integer.class);
        return res;
    }
}
